<?php

	$this->Html->script(array('jquery.maskedinput-1.2.2.min','maskinput','jquery.ui.datepicker-pt-BR','datepicker'), false);
	
	echo "<fieldset><legend>Lista de Espera</legend>";
	echo $this->Form->create('ListaEspera');
	echo $this->Form->input('id');
	echo $this->Form->input('paciente_id',array('type' => 'hidden'));
	echo $this->Form->input('medico_id',array('type' => 'hidden'));
	echo '<div style="clear:both;"></div>';	
	echo $this->Form->input('paciente_nome', array('type' => 'text', 'label' => __('Paciente'), 'class' => 'data readonly50', 'readonly' => 'readonly'));
	echo $this->Form->input('convenio_id', array('type' => 'select', 'options' => $convenios_paciente, 'empty' => 'Selecione...', 'class' => 'tamanho50','label' => __('Convênios do Paciente')));
	echo '<div style="clear:both;"></div>';
	echo '<fieldset><legend>Telefones do Paciente</legend><span id="span_telefones_paciente" style="font-size:12px;">' . @$telefones_paciente . '</span></fieldset>';	
	echo '<div style="clear:both;"></div>';
	echo $this->Form->input('medico_nome', array('type' => 'text', 'label' => __('Médico'), 'class' => 'tamanho50'));
	echo '<div style="clear:both;"></div>';
	echo '<span style="font-size:10px; color: rgb(255, 0, 0); "> (Senha telefone: '. @$senha_medico .')</span>';
	echo '<div style="clear:both;"></br></div>';
	echo $this->Form->input('data_inclusao', array('label' => __('Data'), 'type' => 'text', 'class' => 'maskdata data','class' => 'data readonly', 'readonly' => 'readonly'));
	echo $this->Form->input('situacao', array('type' => 'select', 'options' => $situacoes, 'empty' => 'Selecione', 'class' => 'tamanho30', 'label' => __('Situação')));
	echo $this->Form->input('primeira_vez', array('type' => 'select', 'options' => $primeira_vez, 'empty' => 'Selecione', 'class' => 'tamanho30', 'label' => __('Primeira Vez')));	
	echo '<div style="clear:both;"></div>';
	echo $this->Form->input('observacao', array('label' => __('Observações'), 'type' => 'textarea', 'maxlength' => 500, 'style' => 'height:100px;width:99%'));
	echo '<div class="botoes_form">';
	echo $this->Form->button('Salvar', array('type' => 'submit'));
	echo $this->Form->button('Voltar', array('type'=>'button', 'onClick' => "javascript:location='{$this->request->base}/{$this->request->controller}/index'"));
	echo '</div>'; 
	echo $this->Form->end();
	echo '</fieldset>';
	
?>

<script>

	$("#ListaEsperaConvenioId").focus();

	$("#ListaEsperaPacienteNome").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: '<?php echo $this->Html->url(array('controller' => 'pacientes', 'action' => 'pesquisar')); ?>',
				dataType: "jsonp",
				data: {
					nome: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							id: item.id,
							value: item.value
						}
					}));
				}
			});
		},
		
		minLength: 3,
		
		select: function( event, ui ) {
			$("#ListaEsperaPacienteId").val(ui.item.id);
			$("#ListaEsperaPacienteNome").val(ui.item.value);
			buscar_telefones_paciente();
			carregar_convenios();

		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});

	$("#ListaEsperaMedicoNome").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: '<?php echo $this->Html->url(array('controller' => 'medicos', 'action' => 'pesquisar')); ?>',
				dataType: "jsonp",
				data: {
					nome: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							id: item.id,
							value: item.value
						}
					}));
				}
			});
		},
		
		minLength: 3,
		
		select: function( event, ui ) {
			$("#ListaEsperaMedicoId").val(ui.item.id);
			$("#ListaEsperaMedicoNome").val(ui.item.value);

		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});	

	function buscar_telefones_paciente() {
		
		$.ajax({
			type: 'POST',
			url: '<?php echo $this->request->base; ?>/lista_esperas/buscar_telefones_paciente/' + $('#ListaEsperaPacienteId').val(),
			async: true,
			beforeSend : function() {
				$('#span_telefones_paciente').html('Pesquisando...');
			},
			success: function(retorno){
				$('#span_telefones_paciente').html(retorno);
			}
		});
				
	}	

	function carregar_convenios() {

		$.ajax({
			type: 'POST',
			url: root() + '/pacientes/get_convenios_list/' + $('#ListaEsperaPacienteId').val(),
			async: true,
			beforeSend : function() {
				$('#ListaEsperaConvenioId').html('<option>Carregando...</option>');
			},
			success: function(retorno){
				$('#ListaEsperaConvenioId').html(retorno);
				buscar_observacoes_agenda();
			}
		});		
		
	}	
	
	
</script>	